package com.sanjiahantang.youjiarole.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import com.gyf.barlibrary.ImmersionBar;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.utils.KeyBoardUtils;

/**
 * Created by zy on 2017/8/31.
 */

public abstract class BaseMvpActivity<V extends BaseView, T extends BaseMvpPresenter<V>> extends AppCompatActivity implements StaticValue {
    public T presenter;
    public BaseMvpActivity $this;
    protected ImmersionBar immersionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = initPresenter();
        presenter.attach((V) this);
        $this = this;
        BaseApplication.addBaseList($this);
        initLoadDialog(this);
        initView();
        if (isImmersionBarEnabled())
            initImmersionBar();
        initData();
        UnCaughtExceptionImp catcher = new UnCaughtExceptionImp();
        catcher.init($this);
        Thread.setDefaultUncaughtExceptionHandler(catcher);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onPause() {
        super.onPause();
        KeyBoardUtils.closeKeybordOnly($this);
    }


    @Override
    protected void onDestroy() {
        presenter.detach();
        super.onDestroy();
        KeyBoardUtils.closeKeybordOnly($this);
        if (immersionBar != null)
            immersionBar.destroy();
        if (mLoadingDialog != null){
            mLoadingDialog.dismiss();
        }
    }

    // 实例化presenter
    public abstract T initPresenter();

    //
    public abstract void initView();

    //
    public abstract void initData();

    protected ProgressDialog mLoadingDialog;

    protected void initLoadDialog(@NonNull Context context) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new ProgressDialog(context);
            mLoadingDialog.setCanceledOnTouchOutside(false);
            mLoadingDialog.setMessage(context.getResources().getString(R.string.common_loading));
        }
    }

    /**
     * 打开数据加载框
     */
    public void showProgress(@NonNull Context context) {
        initLoadDialog(context);
        mLoadingDialog.show();
    }


    /**
     * 关闭数据加载框
     */
    public void hideProgress() {

        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    /**
     * 是否可以使用沉浸式
     * Is immersion bar enabled boolean.
     *
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return true;
    }

    protected void initImmersionBar() {
        //在BaseActivity里初始化
        immersionBar = ImmersionBar.with(this);
        immersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).transparentBar().fullScreen(false).navigationBarAlpha(1.0f) .statusBarDarkFont(true, 0.2f)
                .init();
    }

}
